/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module util.c - Various application utility functions |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Utils/util.c $ 
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/

#include <project.h>                // MUST BE FIRST FILE INCLUDED

#define VSUTIL_C

#include <aclconio.h>
#include <stdlib.h>
#include <stdio.h>
#include <ascii.h>
#include <string.h>
#include <acldev.h>
#include <aclstr.h>
#include <applidl.h>
#include <message.h>

/** /#define DEBUG_vsutil*/
#ifdef DEBUG_vsutil
#define DEBUG_95
#endif

#include <define.h>
#include <batch.h>
#include <ui.h>
#include <msgfuncs.h>
#include <util.h>
#include <spdebug.h>

#include <bdt.h>
#include <cdt.h>
#include <modem.h>
#include <dct.h>
#include <hdt.h>
#include <lct.h>
#include <tblutil.h>
#include <opt.h>
#include <sdt.h>
#include <tct.h>
#include <trt.h>
#include <udt.h>
#include <ict.h>
#include <cct.h>
#include <mit.h>
#include <mht.h>
#include <cct.h>
#include <cst.h>
#include <sct.h>
#include <pct.h>
#include <crt.h>
#include <dpt.h>
#include <dst.h>
#include <fct.h>
#include <ppt.h>
#include <sst.h>

#include <print.h>

/*-------------------------------------------------------------------------
    Function:       
    Description:    
    Parameters:     
    Returns:        
    Notes:          
--------------------------------------------------------------------------*/

void vdVSAbortProc(int inErrCode) {
    char szLocal[DISP_STR_LEN + 1];

    vdWarnBeep();

    sprintf(szLocal, "Internal error: #%03d", inErrCode);
    vdDisplayAt(obDisp.inHomeCol, obDisp.inTitleLine, szLocal, CLR_EOL);
    vdWarnBeep();
    exit(-1);
}

/*-------------------------------------------------------------------------
    Function:       
    Description:    This function is called to get the space left on the file system.
                    The limit is checked against the size of the batch record   
    Parameters:     structure and the size of the key structure.
    Returns:        VS_TRUE  : If memory is available
                    VS_FALSE : If memory s not availble
    Notes:          
--------------------------------------------------------------------------*/

VS_BOOL fMemAvail(long* plnAvailSz) {
    struct TagDIR {
        short inFileCount;  // BJD: changed from int to match struct fs_size in svc.h
        long lnCurSize;
        long lnAvailSize;
    } srDir;

    // BJD: Added "I:" parameter to get Internal memory (static RAM)
    dir_get_sizes("I:", (struct fs_size *) &srDir);

    *plnAvailSz = srDir.lnAvailSize;

    /* KV1 NOTE:  memory/record size pdebugs */
    pdebug(("Avail=%ld,Batch=%d", *plnAvailSz, SIZE_BATCH_REC));
    pdebug(("BDT=%d", SIZE_BDT_REC));
    pdebug(("CCT=%d", SIZE_CCT_REC));
    pdebug(("CDT=%d", SIZE_CDT_REC));
    pdebug(("CPT=%d", SIZE_CPT_REC));
    pdebug(("CRT=%d", SIZE_CRT_REC));
    pdebug(("CST=%d", SIZE_CST_REC));
    pdebug(("DCT=%d", SIZE_DCT_REC));
    pdebug(("DPT=%d", SIZE_DPT_REC));
    pdebug(("DST=%d", SIZE_DST_REC));
    pdebug(("FCT=%d", SIZE_FCT_REC));
    pdebug(("HDT=%d", SIZE_HDT_REC));
    pdebug(("ICT=%d", SIZE_ICT_REC));
    pdebug(("LCT=%d", SIZE_LCT_REC));
    pdebug(("MHT=%d", SIZE_MHT_REC));
    pdebug(("MIT=%d", SIZE_MIT_REC));
    pdebug(("OPT=%d", SIZE_OPT_REC));
    pdebug(("PCT=%d", SIZE_PCT_REC));
    pdebug(("PPT=%d", SIZE_PPT_REC));
    pdebug(("SCT=%d", SIZE_SCT_REC));
    pdebug(("SDT=%d", SIZE_SDT_REC));
#ifdef SPDH_DEFINED    
    pdebug(("SST=%d", SIZE_SST_REC));
#endif    
    pdebug(("TCT=%d", SIZE_TCT_REC));
    pdebug(("TRT=%d", SIZE_TRT_REC));
    pdebug(("UDT=%d", SIZE_UDT_REC));
    /**/

    if (srDir.lnAvailSize > (SAFE_LIMIT * SAFE_SIZE))
        return(VS_TRUE);

    return(VS_FALSE);
}

/*-------------------------------------------------------------------------
    Function    : strlcpy
    Description : Copy string2 to string1 and adds a NULL_CH at the end.
    Parameters  : string 1 = destination string
                  string 2 = source string
                  count = nbr. of bytes to copy

    Returns     : pointer to dest. string
    Globals     : none
    Notes       : none

    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   07/03/95 Ana_C1       Added to vsutil, courtesy of Henry_G1
--------------------------------------------------------------------------*/

char* strlcpy(char* szString1,char* szString2,int count) {
    int i;

    i = 0;
    while (i < count && szString2[i] != '\0') {
        szString1[i] = szString2[i];
        i++;
    }
    szString1[i] = '\0';

    return(szString1);
}


char* strupr(char* szString) {
    char* pchChar;

    pchChar = szString;
    while (*pchChar) {
        char chChar;

        chChar = *pchChar;
        if (chChar >= 'a' && chChar <= 'z')
            chChar -= 32;
        *pchChar = chChar;
        pchChar++;
    }

    return(szString);
}


/*-----------------------------------------------------------------------------
* Routine Name : vdChoiceStrNoCpy()
* Description  : performs a safe strcpy by putting a NULL_CH terminator at the
*                end of the string when a '\0' or a '~' is encountered.
* Parameters   : pchDest = pointer to destination buffer
*                pchSrc = pointer to source buffer
*                inMax = maximum number of characters in string
*
* Returns      :
* Notes        :
*-----------------------------------------------------------------------------
*/
int inChoiceStrNoCpy(char* pchDest,char* pchSrc,int inMax) {
    inMax--;
    while ((*pchSrc != '\0') && (*pchSrc != '~') && (inMax-- > 0)) {
        *(pchDest++) = *(pchSrc++);
    }
    *pchDest = '\0';
    if (*pchSrc == '\0')
        return(VS_ERR);
    else
        return(VS_SUCCESS);
}


/*-----------------------------------------------------------------------------
* Routine Name : vdStrNoCpy()
* Description  : performs a safe strcpy by putting a NULL_CH terminator at the
*                end of the string.
* Parameters   : pchDest = pointer to destination buffer
*                pchSrc = pointer to source buffer
*                inMax = maximum number of characters in string
*
* Returns      :
* Notes        :
*-----------------------------------------------------------------------------
*/
void vdStrNoCpy(char* pchDest,char* pchSrc,int  inMax) {
    inMax--;
    while (*pchSrc != '\0' && inMax-- > 0)
        *(pchDest++) = *(pchSrc++);
    *pchDest = '\0';
}

/*-----------------------------------------------------------------------------
* Routine Name : vdStrNoCpyChar()
* Description  : performs a safe memset by putting a NULL_CH terminator at the
*                end of the string. It pads inMax characters into the string
*                pointed to by pchDest with the character pchSrc
* Parameters   : pchDest = pointer to destination buffer
*                pchSrc = pointer to source char
*                inMax = maximum number of characters in string
*
* Returns      :
* Notes        :
*-----------------------------------------------------------------------------
*/
void vdStrNoCpyChar(char* pchDest,char* pchSrc,int  inMax) {
    inMax--;
    while (inMax-- > 0)
        *(pchDest++) = *(pchSrc);
    *pchDest = '\0';
}


/*-----------------------------------------------------------------------------
* Routine Name : inStrPos()
* Description  : performs a string search of a char. It returns the position
*                of the character in the string. If not found it returns -1.
* Parameters   : pchDest = pointer to destination buffer
*                chSrc   = character to search for
*                inMax   = maximum number of characters in string
*
* Returns      :
* Notes        :
*-----------------------------------------------------------------------------
*/
int inStrPos(char* pchDest,char chSrc,int  inMax) {
    int inCntr = 0;
    inMax--;
    while (inMax-- > 0) {
        if (*(pchDest++) == chSrc)
            return(inCntr);
        inCntr++;
    }
    return(-1);
}

/*-------------------------------------------------------------------------
    Function    : pchGetMessage
    Description : Read message file and return the value read.
    Parameters  : inMsgIndex  message index (msg.h)

    Returns     : pointer to message string
    Globals     : pchMsgBuffer, BASE_MESSAGE_FILE
    Notes       : none

    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   05/14/97  KC_C1     function created
--------------------------------------------------------------------------*/

char* pchGetMessage(long lnMessageIndex) {
    /*    Message Buffer, read from message file, put it here */
    static char pchMessageBuffer[30];

    vdGetMessageFromFile(lnMessageIndex, pchMessageBuffer);

    return(pchMessageBuffer);
}

int get_env_int(const char* tag) {
    int ret = -1;
    char buf[6];

    memset(buf, 0, sizeof(buf));
    if (get_env(tag, buf, sizeof(buf)) > 0)
        ret = atoi(buf);

    return ret;
}

void put_env_int(char* tag,int value) {
    char buf[6];

    memset(buf, 0, sizeof(buf));
    int2str(buf, value);
    put_env(tag, buf, strlen(buf));
}

/*-------------------------------------------------------------------------
    Function    : void vdMlPadChar(char *, char *, int, int, char)
    Description : Pads the buufer with the 2 byte addess of the character
                : specified.
    Parameters  : pchDBuff => The destination buffer
                : pchBuff  => The source buffer
                : inLen    => The total length of the buffer required
                : inType   => The type of justification required
                    ML_RIGHT -> Right justification
                    ML_LEFT  -> Left justification
                : ch       => Character to be used for padding.
    Returns     : None
    Globals     : None
    Notes       : Right now the padding function is designed to be used for the 
                : Multilingual display and can support buffers up to size 16.
                : It is assumed that we are using a bigfont file and the control
                  character address starts with 0x0101

    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      
--------------------------------------------------------------------------*/

void vdMlPadChar(char* pchDBuff,char* pchBuff,int inLen,int inType,char ch) {
    char szTemp[ML_2B_MAX_MSG_SIZE + 1];
    int inTemp,
    inLng,
    inI;

    memset(szTemp, 0x01, sizeof(szTemp));
    inTemp = sizeof(szTemp) - 1;
    for (inI = 1; inI <= inTemp; inI += 2)
        szTemp[inI] = ch;
    szTemp[inTemp] = ML_NULL;

    inLng = strlen(pchBuff);

    if (inLng >= inLen)
        return;

    inTemp = inLen - inLng;

    if (inType == ML_RIGHT) {
        memcpy(&szTemp[inTemp], pchBuff, inLng);
        szTemp[inLen] = ML_NULL;
        strcpy(pchDBuff, szTemp);
    } else if (inType == ML_LEFT) {
        memcpy(&pchDBuff[inLng], szTemp, inTemp);
        pchDBuff[inLen] = ML_NULL;
    }
    return;
}

int inMLstrlen(char* szBuf) {
    int inLength;

    inLength = strlen(szBuf);
    if ((getgrid() == 0) && (get_font_mode() == 2))
        inLength = (inLength / 2) + (inLength % 2);

    return (inLength);
}       

#undef VSUTIL_C

